﻿using System;

using System.Collections.Generic;
using System.Text;
using System.IO;

namespace RSSRadio.Model.priv
{
    class SubscriptionFlatFileHandler: ISubscriptionFileHandler
    {
        #region ISubscriptionFileHandler Members

        
        private string txtLoc;

        public SubscriptionFlatFileHandler()
        {
            txtLoc = Globals.AppSettings.getSettingsFolder() + "\\Subscriptions.txt";
        }

        public bool write(IEnumerable<Subscription> values)
        {
            TextWriter tw = new StreamWriter(txtLoc);
            foreach (Subscription s in values)
            {
                tw.WriteLine(s.URL + "\t" + 
                    s.Title.Replace("\t", " ")  + "\t" +
                    s.RetnetionDays.ToString()  + "\t" +
                    ((s.lastDownloadedEpisodeUrl != "" && s.lastDownloadedEpisodePublishDate != "")?
                    (s.lastDownloadedEpisodeUrl + "\t" + s.lastDownloadedEpisodePublishDate):
                    "")
                    );
            }
            tw.Close();
            return true;
        }

        public bool read(Dictionary<string, Subscription> values, ISubscriptionUpdateListener listener)
        {
            if (File.Exists(txtLoc))
            {
                char[] separators = new char[] { '\t' };
                TextReader tr = new StreamReader(txtLoc);
                try
                {
                    String s;
                    Subscription sub;
                    s = tr.ReadLine();
                    lock (values)
                    {
                        values.Clear();
                        while (s != null)
                        {
                            string[] res = s.Split(separators);
                            sub = new Subscription();
                            if (res.Length > 4)
                            {
                                sub.lastDownloadedEpisodeUrl = res[3];
                                sub.lastDownloadedEpisodePublishDate = res[4];
                            }
                            if (res.Length > 2)
                            {
                                sub.RetnetionDays = Int32.Parse( res[2]);
                            }
                            if (res.Length > 1)
                            {
                                sub.Title = res[1];
                            }
                            if (res.Length > 0)
                            {
                                sub.URL = res[0];
                            }
                            else
                            {
                                //Error skip this line
                            }
                            values.Add(sub.URL, sub);
                            sub.addListener(listener);
                            s = tr.ReadLine();
                        }
                    }
                }
                catch
                {
                    return false;
                }
                tr.Close();
                return true;

            }
            else
                return false;
        }

        #endregion
    }
}
